package com.sulent.frame.web.controller.test;

import cn.hutool.json.JSONUtil;
import com.sulent.frame.web.controller.test.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName KafkaController
 * @Description: TODO
 * @Author Submerge.
 * @Since 2025/6/13 11:44
 * @Version 1.0
 */
@RestController
@RequestMapping("/test/kafka")
public class KafkaController {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    /**
     * kafka生产者发送消息
     *
     * @return
     */
    @RequestMapping("/sendStr")
    public ResponseEntity<String> sendStr() {
        for (int i = 0; i < 9; i++) {
            kafkaTemplate.send("sulent-frame-web-str","str-"+i, "hello kafka"+i);
        }
        return ResponseEntity.ok("success");
    }

    @RequestMapping("/sendObj")
    public ResponseEntity<String> sendObj() {
        UserVo userVo = null;
        for (int i = 0; i < 3; i++) {
            userVo = new UserVo();
            userVo.setId("id"+i);
            userVo.setName("name-"+i);
            userVo.setUserName("user-"+i);
            userVo.setPhone("phone-"+i);
            userVo.setSex(i);
            kafkaTemplate.send("sulent-frame-web-obj","obj-"+i, JSONUtil.toJsonStr(userVo));
        }
        return ResponseEntity.ok("success");
    }

}
